package com.hxzy.shop.controller.market;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.hxzy.shop.dto.marketing.dto.req.*;
import com.hxzy.shop.dto.marketing.dto.resp.DistributionDrugListResp;
import com.hxzy.shop.dto.marketing.dto.resp.DistributionDrugResp;
import com.hxzy.shop.dto.marketing.dto.resp.DistributionGlobalAndBindRuleResp;
import com.hxzy.shop.entity.DistributionLevel;
import com.hxzy.shop.feign.marketing.MarketingFeign;
import com.hxzy.shop.vo.ApiException;
import com.hxzy.shop.vo.Result;
import com.hxzy.shop.vo.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ Author     ：dididi
 * @ Date       ：Created in 11:44 2023/8/2
 * @ Modified By：
 */
@Api(tags = "分销")
@RestController
@RequestMapping(value = "/distribution")
public class DistributionController {
    @Resource
    private MarketingFeign marketingFeign;
    @ApiOperation(value = "分销等级列表")
    @PostMapping(value = "/levelList")
    public Result<List<DistributionLevel>> levelList(){
        Result<List<DistributionLevel>> result = marketingFeign.levelList();
        if (result.getCode()!= ResultCode.SUCCESS.getCode()){
            throw new ApiException(500, result.getMsg());
        }
        return Result.success(result.getData());
    }

    @ApiOperation(value = "分销全球规则")
    @PostMapping(value = "/globalRule")
    public Result<DistributionGlobalAndBindRuleResp> globalRule(){
        Result<DistributionGlobalAndBindRuleResp> result = marketingFeign.globalRule();
        if (result.getCode() != ResultCode.SUCCESS.getCode()){
            throw new ApiException(500, result.getMsg());
        }
        return Result.success(result.getData());
    }

    @PostMapping(value = "/globalRule/update")
    @ApiOperation(value = "编辑分销全局规则")
    public Result<Object> globalRuleUpdate(@RequestBody DistributionGlobalRuleUpdateReq req){
        Result<Object> result = this.marketingFeign.globalRuleUpdate(req);
        if (result.getCode() != ResultCode.SUCCESS.getCode()){
            throw new ApiException(500,result.getMsg());
        }
        return Result.success(result.getData());
    }

    @ApiOperation(value = "分销等级管理")
    @PostMapping(value = "/levelSave")
    public Result<Object> levelSave(@RequestBody List<DistributionLevelSaveReq> reqs){
      Result<Object> result = this.marketingFeign.levelSave(reqs);
      if (result.getCode() != ResultCode.SUCCESS.getCode()){
          throw new ApiException(500, result.getMsg());
      }
      return Result.success(result.getData());
    }


    @PostMapping(value = "/drugList")
    @ApiOperation(value = "分销药品列表")
    public Result<PageInfo<DistributionDrugListResp>> drugList(@RequestBody DistributionDrugPageReq req){
       Result<PageInfo<DistributionDrugListResp>> result = this.marketingFeign.drugList(req);
       if (result.getCode() != ResultCode.SUCCESS.getCode()){
           throw new ApiException(500, result.getMsg());
       }
       return Result.success(result.getData());
    }

    @PostMapping(value = "/noSelect/drugList")
    @ApiOperation(value = "分销未选药品列表")
    public Result<PageInfo<DistributionDrugResp>>NoSelectDrugList(@RequestBody DistributionDrugNoSelectReq req){
        Result<PageInfo<DistributionDrugResp>> result = this.marketingFeign.noSelectdrugList(req);
        if (result.getCode()!=ResultCode.SUCCESS.getCode()){
            throw new ApiException(500,result.getMsg());
        }
        return Result.success(result.getData());
    }

    @PostMapping(value = "/drug/update")
    @ApiOperation(value = "修改分销药品")
    public Result<Object>updateDistributionDrug(@RequestBody @Validated UpdateDistributionDrugReq req ){
        Result<Object>result = this.marketingFeign.updateDistributionDrug(req);
        if (result.getCode()!=ResultCode.SUCCESS.getCode()){
            throw new ApiException(500,result.getMsg());
        }
        return Result.success(result.getData());
    }

    @PostMapping(value = "/drug/delete")
    @ApiOperation(value = "删除分销药品")
    public Result<Object> deleteDistributionDrug(@RequestBody @Validated DeleteDistributionDrugReq req){
        Result<Object> result = this.marketingFeign.deleteDistributionDrug(req);
        if (result.getCode() != ResultCode.SUCCESS.getCode()){
            throw new ApiException(500,result.getMsg());
        }
        return Result.success(result.getData());
    }

    @PostMapping(value = "/drug/add")
    @ApiOperation(value = "添加分销药品")
    public Result<Object> addDistributionDrug(@RequestBody AddDistributionDrugReq req){
        Result<Object> result = this.marketingFeign.addDistributionDrug(req);
        if (result.getCode()!=ResultCode.SUCCESS.getCode()){
            throw new ApiException(500,result.getMsg());
        }
        return Result.success(result.getData());
    }

}
